package com.chookss.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;

import com.chookss.R;

import butterknife.ButterKnife;
import io.reactivex.annotations.NonNull;


public abstract class ListBottomDialog extends Dialog {
    public ListBottomDialog(@NonNull Context context) {
        super(context);
        initLayoutRes();
    }

    public ListBottomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initLayoutRes();
    }


    protected abstract @LayoutRes
    int getLayoutId();

    protected abstract void init(View view);


    private void initLayoutRes() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(getLayoutId(), null);
        ButterKnife.bind(this, view);
        setContentView(view);
        initBottom();
        init(view);
    }

    protected void initBottom() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        Window wm = getWindow();
        lp.width = lp.MATCH_PARENT;
        lp.height = lp.WRAP_CONTENT;
        lp.dimAmount = 0f;
        wm.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        wm.setGravity(Gravity.BOTTOM);
        wm.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled);
        getWindow().setAttributes(lp);
    }
    protected void safeDismiss() {
        if (isShowing()) {
            dismiss();
        }
    }

}
